package com.chzc.fm.duotin.ui.channel;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.adapter.recyclerview.BottonEditTypeAdapter;
import com.chzc.fm.duotin.adapter.recyclerview.EditTypeAdapter;
import com.chzc.fm.duotin.bean.channel.ChannelType;
import com.chzc.fm.duotin.utils.ACache;
import com.chzc.fm.duotin.utils.GlobalParam;
import com.chzc.fm.duotin.utils.LogUtil;
import com.chzc.fm.duotin.widget.MyItemTouchCallback;
import com.chzc.fm.duotin.widget.OnRecyclerItemClickListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/7/1.
 */
public class EditTypeActivity extends AppCompatActivity implements View.OnClickListener,MyItemTouchCallback.OnDragListener {

    private RecyclerView toprecyclerView,bottonrecyclerView;
    private TextView tvsave;
    private ImageView ivback;
    private List<ChannelType> toptypeList;
    private EditTypeAdapter topadapter;
    private BottonEditTypeAdapter bottonadapter;

    private ItemTouchHelper itemTouchHelper;

    private static final int RESULT_CODE = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edittype);

        initViews();

        initEvent();

        initDatas();
    }

    public void initViews()
    {
        toprecyclerView = (RecyclerView) findViewById(R.id.edittype_top_rv);
        bottonrecyclerView = (RecyclerView) findViewById(R.id.edittype_botton_rv);

        tvsave = (TextView) findViewById(R.id.edittype_save_tv);
        ivback = (ImageView) findViewById(R.id.edittype_back_iv);

        //顶部RecycleView布局
        GridLayoutManager topmanager = new GridLayoutManager(EditTypeActivity.this,4);
        toprecyclerView.setLayoutManager(topmanager);
        //顶部RecycleView布局
        GridLayoutManager bottonmanager = new GridLayoutManager(EditTypeActivity.this,4);
        bottonrecyclerView.setLayoutManager(bottonmanager);
    }

    public void initEvent()
    {
        tvsave.setOnClickListener(this);
        ivback.setOnClickListener(this);
    }

    public void initDatas()
    {

        //初始化数据，如果缓存中有就使用缓存中的
        final ArrayList<ChannelType> type = (ArrayList<ChannelType>) ACache.get(EditTypeActivity.this).getAsObject("typelist");

        if(type != null)
        {
            toptypeList.addAll(type);
        }else {
            toptypeList = (List<ChannelType>) getIntent().getSerializableExtra("typeList");
        }

        LogUtil.w("typeList.size()" + toptypeList.size());

        topadapter = new EditTypeAdapter(EditTypeActivity.this,toptypeList);
        toprecyclerView.setAdapter(topadapter);

        bottonadapter = new BottonEditTypeAdapter(EditTypeActivity.this,GlobalParam.bottontypeList);
        bottonrecyclerView.setAdapter(bottonadapter);

        itemTouchHelper = new ItemTouchHelper(new MyItemTouchCallback(topadapter).setOnDragListener(this));
        itemTouchHelper.attachToRecyclerView(toprecyclerView);

        toprecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(toprecyclerView)
        {
            @Override
            public void onLongClick(RecyclerView.ViewHolder vh, int psoition) {
                super.onLongClick(vh, psoition);

                Animation shake = AnimationUtils.loadAnimation(EditTypeActivity.this,R.anim.shake);
                toprecyclerView.startAnimation(shake);
            }

            @Override
            public void onItemClick(RecyclerView.ViewHolder vh, int position) {
                super.onItemClick(vh, position);

                ChannelType topchannelType = toptypeList.get(position);
                Toast.makeText(EditTypeActivity.this,"你点击的是"+position+"个" + "title>>>" + toptypeList.get(position).getTitle(),Toast.LENGTH_SHORT).show();
                toptypeList.remove(position);
                topadapter.notifyDataSetChanged();

                GlobalParam.bottontypeList.add(topchannelType);
                bottonadapter.notifyDataSetChanged();
            }
        });
        bottonrecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(bottonrecyclerView)
        {
            @Override
            public void onItemClick(RecyclerView.ViewHolder vh, int position) {
                super.onItemClick(vh, position);

                ChannelType bottonchannelType =  GlobalParam.bottontypeList.get(position);
                GlobalParam.bottontypeList.remove(position);
                bottonadapter.notifyDataSetChanged();

                toptypeList.add(toptypeList.size()-1,bottonchannelType);
                topadapter.notifyDataSetChanged();
            }
        });

    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.edittype_save_tv:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("typelist", (Serializable) toptypeList);
                intent.putExtras(bundle);
                setResult(RESULT_CODE,intent);
                finish();
                Toast.makeText(EditTypeActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                break;
            case R.id.edittype_back_iv:
                GlobalParam.bottontypeList.clear();
                finish();
                break;
        }
    }

    @Override
    public void onFinishDrag() {

        //存入缓存
        ACache.get(EditTypeActivity.this).put("typelist",(ArrayList<ChannelType>)toptypeList);
    }



    @Override
    public void finish() {
        for(int i = 0 ; i < toptypeList.size() ; i ++ )
        {
            LogUtil.w("typelist>>>>>" + i +">>>>" + toptypeList.get(i).getTitle());
        }
        super.finish();
    }

}
